package com.code.generator;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.DbColumnType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;

import java.nio.file.Paths;
import java.sql.Types;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * <p>
 *
 * </p>
 *
 * @author chw
 * @date 2025/4/10
 */
public class GenXugu {

    public static final String xugu_url = "jdbc:xugu://203.195.146.38:5138/TPM?current_schema=writeoff&useUnicode=true&characterEncoding=utf-8&useLegacyDatetimeCode=false&serverTimezone=Asia/Shanghai";
    public static final String username = "app_test";
    public static final String pwd = "Qwz3bEAP3D+2yoDf";
    public static final String CREATED_TIME = "created_time";
    public static final String UPDATE_TIME = "updateTime";
    public static final String UPDATED_BY = "updated_by";
    public static final String CREATED_BY = "created_by";
    public static final String ID = "id";
    public static final String VERSION = "version";
    public static final String DELETED = "deleted";

    public static void main(String[] args) {
        System.out.println("Hello, World!");
        GenXugu main = new GenXugu();
        main.test2();
    }


    public void test2() {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入表名，多个英文逗号分隔？所有输入 all");
        FastAutoGenerator.create(xugu_url, username, pwd)
                .globalConfig(builder -> builder
                        .author("Chw")
                        .enableSwagger()
                        .disableOpenDir()// 禁止打开输出目录
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                        .commentDate("yyyy-MM-dd")
                )
                .dataSourceConfig(builder ->
                        builder.typeConvertHandler((globalConfig, typeRegistry, metaInfo) -> {
                            int typeCode = metaInfo.getJdbcType().TYPE_CODE;
                            if (typeCode == Types.SMALLINT) {
                                // 自定义类型转换
                                return DbColumnType.INTEGER;
                            }
                            return typeRegistry.getColumnType(metaInfo);
                        })
                )
                .packageConfig(builder -> builder
                        .parent("com.baomidou.mybatisplus")
                        .moduleName("test")
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                        .pathInfo(Collections.singletonMap(OutputFile.xml
                                , System.getProperty("user.dir") + "/src/main/resources/mapper"))
                )
                .strategyConfig(builder -> {
                            builder.entityBuilder()
                                    .superClass(BaseEntity.class)
                                    .enableLombok()
                                    .enableTableFieldAnnotation()
                                    .enableChainModel()
                                    .enableRemoveIsPrefix()
                                    .addTableFills(new Column(CREATED_TIME, FieldFill.INSERT))
                                    .addTableFills(new Property(UPDATE_TIME, FieldFill.INSERT_UPDATE))
                                    .versionColumnName(VERSION)
                                    .logicDeleteColumnName(DELETED)
                                    .addSuperEntityColumns(ID, CREATED_BY, CREATED_TIME, UPDATED_BY, UPDATE_TIME)
                                    .enableFileOverride()
                            ;
                            builder.addInclude(getTables(scanner.nextLine()))
                                    .controllerBuilder()
                                    .enableRestStyle();
                            builder.controllerBuilder().enableFileOverride();
                            builder.serviceBuilder().enableFileOverride();
                            builder.mapperBuilder().enableFileOverride();

                        }
                )
                .templateEngine(new FreemarkerTemplateEngine())
                .templateConfig(builder -> {
                    builder
//                            .xml("/templates/mapper.xml")
//                            .serviceImpl("/templates/serviceImpl.java")
                            .controller("templates/controller.java")
                            .build();
                })
                .execute();
    }

    // 处理 all 情况
    protected List<String> getTables(String tables) {
        return "all".equals(tables) ? Collections.emptyList() : Arrays.asList(tables.split(","));
    }
}
